    

    customize_reports = {
	save_form_workspace : function(elements){
	    
	    $("body").on("click",elements,function(){
		var form_type = $(this).attr("data-form-type");
                    // Settings
                    var workspace_title = $(".workspace_title").val();
                    var workspace_displayName = $(".workspace_displayName").val();
                    var workspace_width = $(".form_size_width").val();
                    var workspace_height = $(".form_size_height").val();
		    
                    switch (form_type) {
        
                        case "generate":
        
                            if ($(".form-builder-ruler").length >= 1) {
                                $(".form-builder-ruler").remove()
                            }
        
                            // Get Object Length
        
                            var object_length = $(".setObject").length;
                            // Get Button Length
                            var btn_length = $(".setObject_btn").length;
        
        
                            // Condition if workspace is null
                            if (object_length < 1) {
                                // Notification
                                jAlert("Workspace is empty.", "", "", "", "", function() {
                                });
                            } else if (workspace_title == "") {
                                showNotification({
                                    message: "Title is empty.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            } else if (workspace_displayName == "") {
                                showNotification({
                                    message: "Display Name is empty.",
                                    type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                            } else {
                                $(".saveFormLoad").show();
                                var form_Fields_Arr = new Array();
                                // Get Fields Name
                                //    var form_fields = $(".getFields").map(function(){
                                //	return $(this).attr("name"));
                                //    }).get().join();
                                //
                                //
                                $(".getFields").each(function() {
                                    //return $(this).attr("name"));
                                    form_Fields_Arr.push({
                                        fieldName: $(this).attr('name'),
                                        fieldType: $(this).attr('data-type')
                                    });
                                })
                                //console.log(form_fields)
        
                                // Get Button
                                var btnName = $(".setObject_btn").map(function() {
                                    return $(this).children("[data-type='button']").val();
                                }).get().join();
                                // var btnName = $(".setObject_btn").map(function(){
                                //     return $(this).children().attr("name");
                                // }).get().join();
        
                                // Get Workspace Content
                                var workspace_content = $(".workspace").html();
				var pathname = window.location;
				    if (getParametersName("view_type",pathname)=="update"){
					var formId = $('#FormIdPrint').val();
				    }else{
					var formId = $('#FormId').val();
				    }
                                var btnContent = $(".btn_content").html();
    
                                //console.log(btnName)
                                // Set JSON
                                var workspace = {};
                                workspace["Title"] = workspace_title;
                                workspace["DisplayName"] = workspace_displayName;
                                workspace["Prefix"] = "";
                                workspace["Type"] = "";
                                workspace["WorkspaceHeight"] = workspace_height;
                                workspace["WorkspaceWidth"] = workspace_width;
                                workspace['BtnLength'] = btn_length;
                                workspace['BtnName'] = btnName;
                                workspace['button_content'] = btnContent;
                                workspace['ObjectLength'] = object_length;
                                workspace['WorkspaceContent'] = workspace_content;
                                workspace['form_json'] = $("body").data();
                                workspace['form_fields'] = JSON.stringify(form_Fields_Arr);
                                workspace['categoryName'] = "";
                                workspace['formrequestID'] = $("#requestID").val();
                                workspace['formtrackNo'] = $("#trackNo").val();
				workspace['formID'] = formId;
                                workspace['generate_object'] = $(".generate_objects").html();
                                workspace['tab_object'] =  $(".tab_objects").html();
                                var json_a = $("body").data();
                                
                                // workspace["WorkspaceHeight"] = workspace_height;
                                // workspace["WorkspaceWidth"] = workspace_width;
                                if(workspace["WorkspaceHeight"] == "" || workspace["WorkspaceHeight"] == null || typeof workspace["WorkspaceHeight"] === "undefined"){
                                    workspace["WorkspaceHeight"] = $('.workspace').height();
                                }
                                if(workspace["WorkspaceWidth"] == "" || workspace["WorkspaceWidth"] == null || typeof workspace["WorkspaceWidth"] === "undefined"){
                                    workspace["WorkspaceHeight"] = $('.workspace').width();
                                }
                                if (typeof json_a.saving_request_format != "undefined") {
                                    workspace['MobileJsonData'] = JSON.stringify(json_a.saving_request_format);
                                } else {
                                    workspace['MobileJsonData'] = "";
                                }
        
                                if (typeof json_a.saved_mobile_content != "undefined") {
                                    workspace['MobileContent'] = json_a.saved_mobile_content;
                                } else {
                                    workspace['MobileContent'] = "";
                                }
                                /*collecting existing fields on the form*/
                                workspace['existing_fields'] = [];
                                $(".setObject").each(function(eqi_setObj) {
                                    if (
                                            $(this).attr("data-type") == "labelOnly" ||
                                            $(this).attr("data-type") == "createLine" ||
                                            $(this).attr("data-type") == "table" ||
                                            $(this).attr("data-type") == "tab-panel" ||
                                            $(this).attr("data-type") == "listNames"
                                            ) {
                                        return true; //preventing to insert it to the database
                                    }
                                    counted_id = $(this).attr("data-object-id");
                                    if ($(this).find(".getFields_" + counted_id).attr("name")) {
                                        workspace['existing_fields'].push($(this).find(".getFields_" + counted_id).attr("name").replace("[]", ""));
                                    }
                                });
                                workspace['existing_fields'] = workspace['existing_fields'].join(",");
                                
                                var json_encode = JSON.stringify(workspace);
                                //console.log(json_encode)
                                $.post("/ajax/generate", {
                                    action: "saveWorkspace",
                                    json_encode: json_encode,
                                    formId: formId,
				    type : $("#saveType").val()
                                }, function(data) {
                                    if (data) {
                                        data = JSON.parse(data)
                                        if (data['status'] == "success") {
                                            $("#popup_overlay,#popup_container").remove();
                                            bind_onBeforeOnload(0)
                                            showNotification({
                                                message: "Customize Report was successfully saved.",
                                                type: "success",
                                                autoClose: true,
                                                duration: 3
                                            });
					    window.location.replace(data['redirectURL']);
                                        }
                                        $(".saveFormLoad").hide();
                                    }
                                })
                            }
                            break;
        
                    }
	    });
	},
        
        
        load_form_generate : function(){
            var pathname = window.location.pathname;
	    var user_view = $("#user_url_view").val();
	    $('.generate_objects').perfectScrollbar('destroy');
            if (pathname == "/generate" || pathname == "/user_view/generate") {
		
                var path = window.location;
		
                if (getParametersName("view_type", path) == "update"){
		    
                    $.ajax({
                        type    :   "POST",
                        url     :   "/ajax/generate",
                        dataType:   'json',
                        data    :   {action:"loadWorkspace",generateID:getParametersName("formID", path)},
                        cache   :   false,
                        success :   function(e){
                          //console.log("asdsa");
			  //console.log(e[0].generate_object)
			    
                            $(".generate_objects").append(e[0].generate_object);
                            $(".tab_objects").html(e[0].tab_object);
			    $('.generate_objects').perfectScrollbar();
			    var sLength = $('.ps-scrollbar-y').length;
			    var xLength = $('.ps-scrollbar-x').length;
			    var l = parseInt(sLength)-2;
			    $('.ps-scrollbar-y:eq('+ l +')').remove();
			    $('.ps-scrollbar-x').remove();
			    
			    var collect_counter_id = [];
			    $('.workspace.formbuilder_ws').find('.setObject').each(function(){
				 var dis_ele = $(this);
				 var dis_ele_id = dis_ele.attr('data-object-id');
				 collect_counter_id.push(Number(dis_ele_id));
			    });
			    collect_counter_id.sort(function(a,b){
				 return b-a;
			    });
			    count = collect_counter_id[0];
                        }
                    });
                }
                
		
                $(".object_properties").each(function(){
                    $(this).remove();
                });
                $(".object_remove").each(function(){
                    $(this).addClass("hideObjectFields");
		     
                    $(this).removeClass("object_remove");
                    $(this).show();
                    $(this).removeClass("display");
                });
		
		$(".hideObjectFields").each(function(){
		    $(this).addClass("fa fa-times");
                });
		
                
                // Table Properties
                $(".table-actions-ra").each(function(){
                    $(this).remove();    
                });
                
                // Remove Fields
                $("body").on("click",".hideObjectFields",function(){
                    var dataID = $(this).attr("data-object-id");
                    var object_name = $("#lbl_" + dataID).html();
                    //$("#setObject_" + dataID).hide();
                    $(this).parent().parent().parent().parent().hide();
                    var ret = '<button class="fl-command-btn btn-basicBtn padding_5 cursor showObjectFields dataTip" data-original-title="' + object_name + '" id="showObjectFields_' + dataID + '" data-object-id="' + dataID + '" style="margin-right:2px;margin-top: 5px;width: 49%;font-size: 11px;"><i class="icon-cogs"></i> ' + object_name.substr(0, 10) + '...' + '</button>';
                    
                    $(".generate_objects").append(ret);
                    $(".dataTip").tooltip();
                    $(".generate_objects").perfectScrollbar();
                    $(".leftPointerRuler-tip").css("top",mostBottomShowTab($(".workspace").children(".setObject"))[0] + "px");
                });
                
                // Show / Add Fields
                $("body").on("click",".showObjectFields",function(){
                    var dataID = $(this).attr("data-object-id");
                    $(".dataTip").tooltip('destroy');
                    $("#showObjectFields_" + dataID).remove();
                    $("#setObject_" + dataID).show();
		    $('html, body').animate({
			scrollTop: $("#setObject_" + dataID).offset().top
		    }, 'fast');
                    $("#setObject_" + dataID).css({ "border":"1px solid",
						    "border-color":"red",
						    "z-index":"2"});
		    
                });
                addRemoveTable(".formbuilder_ws");
                // Check if there's a tab in the form
                if ($(".form-tabbable-pane").length!=0) {
                    
                    
                   var  ret = "";
                   //var tab_html="";
                   var count1 = 1;
                   $(".form-tabbable-pane ul li").each(function(id,val){
                        var tab = $(this).attr("aria-controls");
                        var tab_name = $(this).children().html();
                        var label_id = $(this).attr("aria-labelledby");
                        
                        var tab_width = $("div[data-type='tab-panel']").width();
                        var tab_height = $("div[data-type='tab-panel']").height();
			var a = parseInt(count1);
                        
                        //<div style="position:absolute;right:0;z-index: 111111;"><i class="icon-remove"></i></div>
                        var tab_html = '<div class="showTabObjectGet setObject cursor_move ui-draggable ui-resizable"  data-tab-name="' + tab + '" data-type="tab-panel" data-object-id="' + count1 + '" id="setObject_tab_' + count1++ + '" data-toggle="popover" data-placement="right" style=" border-color: rgb(221, 221, 221); float: none; position: absolute; width: '+ tab_width + 'px; height: '+ tab_height + 'px;">';
			    tab_html += '<div class="setObjects_actions"><div class="obj_actions display" id="obj_actions_1_' + a + '" style="display: none;"><i class="icon-remove cursor removeTabObjects" data-tab-name="' + tab + '"  data-object-id="' + a + '" data-original-title="Remove" ></i></div></div>';
			    tab_html +=  $("#" + tab).html();
			    tab_html += '</div>';
                     
                       if (tab!="blank-new-tab-panel") {
                          ret += '<button class="fl-command-btn btn-basicBtn padding_5 cursor getTabValue dataTip" data-original-title="' + tab_name + '" data-height="'+tab_height+'" data-objects-tab="'+ tab_html.replace(/"/g, "///\/") +'" data-tab-name="' + tab + '" id="' + label_id + '" data-object-id="' + label_id + '" style="margin-right:2px;margin-top: 5px;width: 49%;font-size: 11px;"><i class="icon-cogs"></i> ' + tab_name.substr(0, 10) + '</button>';
                       }
                       
		       count1++
                   });
                   $(".tab_objects").append(ret);
                   $("div[data-type='tab-panel']").remove();
                   $(".tab_objects").perfectScrollbar();
                   $(".dataTip").tooltip();
                   
                }
               
                // Get Tab Value HTMl
                var count = 1;
                $("body").on("click",".getTabValue",function(){
                    var getTab = $(this).attr("data-tab-name");
        		    $(this).hide();
        		    var objectID = $("div[data-type='tab-panel']").attr("data-object-id");
                            var tab_width = $("div[data-type='tab-panel']").width();
        		   
                            var tab_height = $("div[data-type='tab-panel']").height();
        		    var dataHeight = $(this).attr("data-height");
        		    var top_ruler = $(".leftPointerRuler-tip").css("top");
        		    var left_ruler = $(".topPointerRuler-tip").css("left");
                    //var tab_html = '<div class="setObject cursor_move ui-draggable ui-resizable" data-type="tab-panel" data-object-id="' + count++ + '" id="setObject_tab_' + count++ + '" data-toggle="popover" data-placement="right" style=" border-color: rgb(221, 221, 221); float: none; position: absolute; width: '+ tab_width + 'px; height: '+ tab_height + 'px;">';
                    //    tab_html += $("#" + getTab).html();
                    //tab_html += '</div>';
                    //$(".formbuilder_ws").append(tab_html);
		    
                    // Row
                    
                    
                    var tabhtml = $(this).attr("data-objects-tab");
		   
                    //console.log(tabhtml.replace(/\/\/\/\//g, '"'))
                    var ele_tabhtml = $(tabhtml.replace(/\/\/\/\//g, '"').replace(/\/\/\/>/g, '"'));
                    $(".formbuilder_ws").append(ele_tabhtml);
                    //console.log(mostBottomShowTab($(ele_tabhtml).children(".setObject"))[0]);
                    ele_tabhtml.css({
                        "height":mostBottomShowTab($(ele_tabhtml).children(".setObject"))[0] + "px"
                    })
		    
                    addRemoveTable(".showTabObjectGet");
                    console.log($(ele_tabhtml).children(".thisDynamicTable > tr").length);
        		    $("div[data-tab-name='"+getTab+"']").css({"top":top_ruler,
        						    "left":0});
        		    $(".leftPointerRuler-tip").css("top",mostBottomShowTab($(".workspace").children(".setObject"))[0] + "px");
                    $(".setObject").draggable({containment:"parent",
            			// drag: function(event, ui) {
            			//     // Show Ruler guide                   
            			//     var drag_ele_current_pos_left = ui.offset.left
            			//     var drag_ele_current_pos_top = ui.offset.top
            		
            			//     dragShowVerticalRuler($(ui.helper), drag_ele_current_pos_left, drag_ele_current_pos_top, ".workspace");
            			//     dragShowHorizontalRuler($(ui.helper), drag_ele_current_pos_left, drag_ele_current_pos_top, ".workspace");
            			// },
            			// stop: function(){
            			//     removeRuler(); // Remove Rule after drag stop
            			// }
                    });
                    console.log("HOYYSSs")
                    console.log(ele_tabhtml)
                    //>??? DITO NA AKO
                    // $(".setObject").resizable({
                    //     "handles":"s,e"
                    // });

                    ele_tabhtml.children(".setObject").removeClass("ui-resizable");
                    ele_tabhtml.children(".setObject").find(".ui-resizable-handle").remove();
                    ele_tabhtml.children(".setObject").resizable({
                        "handles":"s"
                    });
                    // Tab Droppable
                    $(".showTabObjectGet").droppable({
                        greedy : true
                        });
                    $("div[data-type='tab-panel']").resizable();
                    //dragObects(".setObject", ".workspace", $("." + "workspace").height, count++)
                    
                    //var ret = $("div[data-type='tab-panel']").html();
                    //$(".formbuilder_ws").append(ret);
                    $(".showTabObjectGet").parents(".ui-resizable").eq(0).resizable("option","minHeight",checkParentResizableMinHeight($(".showTabObjectGet")))
                    $(".showTabObjectGet").parents(".ui-resizable").eq(0).resizable("option","minWidth",checkParentResizableMinWidth($(".showTabObjectGet")))
                    $(".showTabObjectGet").parents(".ui-resizable").eq(0).css("min-height",(checkParentResizableMinHeight($(".showTabObjectGet")))+"px")
                    $(".showTabObjectGet").parents(".ui-resizable").eq(0).css("min-width",(checkParentResizableMinWidth($(".showTabObjectGet")))+"px")
                
		    $(".form_size_height").val($(".workspace").height());
                });
                // Remove tab objects
		$("body").on("click",".removeTabObjects",function(){
		    var getTab = $(this).attr("data-tab-name");
		    $(this).parent().parent().parent().remove();
		    $("button[data-tab-name='"+getTab+"']").show();
                    
                    $(".leftPointerRuler-tip").css("top",mostBottomShowTab($(".workspace").children(".setObject"))[0] + "px");
		    // WOrkspace
		    $(".workspace").css("min-height",$(".leftPointerRuler-tip").position().top);
		});
		
                $("body").on("mouseenter",".setObject",function(){
                    var dataID = $(this).attr("data-object-id");
                    $(this).css("border-color","rgb(221, 221, 221)");
                    $("#obj_actions_1_" + dataID).show();
		    $("#obj_actions_" + dataID).show();
                });
                $("body").on("mouseleave",".setObject",function(){
                    var dataID = $(this).attr("data-object-id");
                    $(this).css("border-color","transparent");
                    $("#obj_actions_1_" + dataID).hide();
		    $("#obj_actions_" + dataID).hide();
                });
                
                
                
                
                // Show Dynamic Table
                $("body").on("click",".showDynamicTable",function(){
                    var dataID = $(this).attr("data-object-id");
                    var type = $(this).attr("data-type-view");
                    var title = $(this).attr("data-original-title");
                    $(".dataTip").tooltip('destroy');
                    
                    if (type=="row") {
                        $("#" + title + "_" + dataID).show();   
                    }else{
                        $("." + title + "_" + dataID).show();   
                    }
                    
                    $(this).remove();
                    
                });
		// Detect objects
		$('html, body').animate({
		    scrollTop: $(".workspace").children().first().offset().top
	    }, 'fast');
        
                $(".setObject").draggable({containment:"parent",
        			// drag: function(event, ui) {
        			//     // Show Ruler guide                   
        			//     var drag_ele_current_pos_left = ui.offset.left
        			//     var drag_ele_current_pos_top = ui.offset.top
        		
        			//     dragShowVerticalRuler($(ui.helper), drag_ele_current_pos_left, drag_ele_current_pos_top, ".workspace");
        			//     dragShowHorizontalRuler($(ui.helper), drag_ele_current_pos_left, drag_ele_current_pos_top, ".workspace");
        			// },
        			// stop: function(){
        			//     removeRuler(); // Remove Rule after drag stop
        			// }
                });
                // Tab Droppable
                $(".showTabObjectGet").droppable({
                    greedy : true
                });
		    
		    
            }
        }
    }
    
    $(document).ready(function(){
	   var pathname = window.location.pathname;
        if (pathname == "/user_view/generate") {
    		$(".workspace").resizable();
            workspace_functions.view_save(".save_workspace");
    	}
        customize_reports.save_form_workspace(".save_form_workspace");
        customize_reports.load_form_generate();
        //resizeForm(".formbuilder_ws", $(".form_size_height").val());
        var pathname = window.location.pathname;
      
            if (pathname == "/workspace") {
                $(".removeRow, .removeCol").hide();
            }
    });
    
    // Functions
    function mostBottomShowTab(elements) {
        var collect = [];
        if ($(elements).length >= 1) {
            $(elements).each(function(eqi){
                collect.push($(this).position().top + $(this).outerHeight());
            })
            collect.sort(function(a,b){
                return b - a;
            })
            
        }else{
            collect.push(0);
        }
        return collect; 
    }
    
    function addRemoveTable(elementAppend) {
        count = 0;
        count_1 = 0;
        $(elementAppend).children(".setObject").each(function(){
            if($(this).find(".form-table.thisDynamicTable").length == 1){
                var ele_table = $(this).find(".form-table.thisDynamicTable");
                
                ele_table.children("tbody").children("tr").each(function(){
                    
                    var append_button = $('<div class="removeRow " style="position:absolute;left:0;z-index: 111111;cursor: pointer;margin-left: -10px;"><i class="icon-remove"></i></div>');
                    
                    $(this).children("td").first().children(".td-relative").prepend(append_button);
                    var nametbl = $(this).parents(".setObject").eq(0).attr("data-object-id");
                    var object_name = $("#lbl_" + nametbl).html().replace(":","");
                    object_name = object_name.replace(new RegExp(" ","g"), "_");
                    
                    $(this).attr({"id":object_name+ "_" + count,
                                 "data-id":count});
                    // Table Tab Name
                    
                    append_button.on({
                        "click.HideMyRow":function(e){
                            $(this).parents("tr").hide();
                            var nametbl = $(this).parents(".setObject").eq(0).attr("data-object-id");
                            var object_name = $("#lbl_" + nametbl).html().replace(":","");
                            object_name = object_name.replace(new RegExp(" ","g"), "_");
                            
                            var dataID = $(this).parents("tr").attr("data-id");
                            var ret = '<button class="btn-basicBtn padding_5 cursor showDynamicTable dataTip" data-type-view="row" data-original-title="' + object_name + '" id="dynamicTable_' + dataID + '" data-object-id="' + dataID + '" style="margin-right:2px;margin-top: 5px;width: 49%;font-size: 11px;"><i class="icon-cogs"></i> ' + object_name.substr(0, 5) + '...' + '</button>';
                    
                            $(".generate_objects").append(ret);
                            $(".generate_objects").perfectScrollbar();
                            $(".dataTip").tooltip();
                            
                            
                        }
                    });
                    count++;
                });
                ele_table.children("tbody").children("tr").first().children("td").first().find(".removeRow").remove();
            }
        })
        
        $(elementAppend).children(".setObject").each(function(){
            if($(this).find(".form-table.thisDynamicTable").length == 1){
                var ele_table = $(this).find(".form-table.thisDynamicTable");
                
                ele_table.children("tbody").children("tr").each(function(eqi){
                    if (eqi == 0) {
                        var append_button = $('<div class="removeCol " style="position:absolute;right:0;z-index: 111111;cursor: pointer;margin-top: -15px;"><i class="icon-remove"></i></div>');
                        append_button.on({
                            "click.HideMyCol":function(e){
                                var hide_colindex = $(this).parents("td").eq(0).index();
                                
                                $(this).parents("tbody").eq(0).children("tr").each(function(){
                                    $(this).children("td").eq(hide_colindex).hide();
                                });
                                
                                var nametbl = $(this).parents(".setObject").eq(0).attr("data-object-id");
                                var object_name = $("#lbl_" + nametbl).html().replace(":","");
                                object_name = object_name.replace(new RegExp(" ","g"), "_");
                                
                                var dataID = $(this).parents("td").eq(0).attr("data-id");
                                var ret = '<button class="btn-basicBtn padding_5 cursor showDynamicTable dataTip" data-type-view="col" data-original-title="col_' + object_name + '" id="dynamicTable_' + dataID + '" data-object-id="' + dataID + '" style="margin-right:2px;margin-top: 5px;width: 49%;font-size: 11px;"><i class="icon-cogs"></i> ' + object_name.substr(0, 5) + '...' + '</button>';
                        
                                $(".generate_objects").append(ret);
                                $(".generate_objects").perfectScrollbar();
                                $(".dataTip").tooltip();
                                
                                
                            }
                        });
                        $(this).children("td").children(".td-relative").prepend(append_button);
                        
                        
                    }
                    var nametbl = $(this).parents(".setObject").eq(0).attr("data-object-id");
                    var object_name = $("#lbl_" + nametbl).html().replace(":","");
                    object_name = object_name.replace(new RegExp(" ","g"), "_");
                    $(this).children("td").each(function(eqi_tdindex){
                        $(this).attr({"id":"col_" + object_name+ "_" + eqi_tdindex,
                                    "data-id":eqi_tdindex});
                        $(this).addClass("col_" + object_name+ "_" + eqi_tdindex);
                       // Table Tab Name
                    })
                    count_1++;
                });
                ele_table.children("tbody").children("tr").first().children("td").first().find(".removeCol").remove();
            }
        })
        
        $(elementAppend).children(".setObject").find(".thisDynamicTable").on({
            "mouseenter.dTableEnter":function(){
                $(this).find(".removeCol, .removeRow").show();
            },
            "mouseleave.dTableLeave":function(){
                $(this).find(".removeCol, .removeRow").hide();
            }
        })
    }
